$(function() {
	$('#submitForm').validate({
		debug: true, //调试模式取消submit的默认提交功能   
        focusInvalid: false, 
        onkeyup: false,   
        submitHandler: function(form){ 
            if (typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') {
            	jQuery.data(this, "disabledOnSubmit", {submited : true});
            	form.submit();
            } else {
            	// repeat submit ignore;
            }
        },
        success:function(element){
        },
		errorPlacement : function(error, element) {
			if(error.text()){
				if(element.next("span[class='red']").size() == 0){
					element.after("<span class='red'>" + error.text() +"</span>");
				}else{
					element.next("span[class='red']").text(error.text());
				}
			}else{
				element.next("span[class='red']").remove();
			}
		},
        rules:{
			name:{required:true},
			description:{required:true},
			enoughMoney:{
				conditionRequired:(function(){
					var type = $("input[name='type']:checked").val();
					if(type == '1'){
						return true;
					}
				}),
				isIntDigit:10
			},
			faceMoney:{required:true,isIntDigit:10},
			validBeginTime:{
				conditionRequired:(function(){
					var validityType = $("input[name='validityType']:checked").val();
					if(validityType == 'absolute'){
						return true;
					}
				})
			},
			validEndTime:{
				conditionRequired:(function(){
					var validityType = $("input[name='validityType']:checked").val();
					if(validityType == 'absolute'){
						return true;
					}
				})
			},
			validDay:{
				conditionRequired:(function(){
					var validityType = $("input[name='validityType']:checked").val();
					if(validityType == 'relative'){
						return true;
					}
				}),
				isIntGtZero:true
			},
			issueTotalAmount:{
				conditionRequired:(function(){
					var limitedType = $("input[name='issueTotalAmountlimited']:checked").val();
					if(limitedType == 'limited'){
						return true;
					}
				}),
				isIntGtZero:true
			},
			bidModel:{
				conditionRequired:(function(){
					var usingRange = $("input[name='usingRange']:checked").val();
					if(usingRange == '2'){
						return true;
					}
				})
			},
			issueBeginTime:{required:true},
			issueEndTime:{required:true},
			issueType:{required:true},
			"issueCondition.rechargeOnceAmount":{
				conditionRequired:(function(){
					var issueType = $("select[name='issueType']").val();
					if(issueType == 2){
						return true;
					}
				}),
				number:true,
				min:0
			},
			"issueCondition.orderAmount":{
				conditionRequired:(function(){
					var issueType = $("select[name='issueType']").val();
					if(issueType == 3){
						return true;
					}
				}),
				number:true,
				min:0
			}
        },
        messages:{
        	bidModel:{required:"请选择交易模式"}
        }
	});
	
	
	//注册事件
	$("input[name='issueTotalAmountlimited']").bind("click",changeIssueTotalAmount);
	$("input[name='validityType']").bind("click",changeValidityType);
	$("select[name='issueType']").bind("change",function(){
		//切换选择清空数据
		$(".dynamic").find("input").val("");
		$("input[name='issueCondition.issueType']").val("");
		changeIssueType();
	});
	
	//初始
	initFormField();
});

function initFormField(){
	changeIssueType();
	changeIssueTotalAmount();
	changeValidityType();
}

function changeIssueType(){
	var issueType = $("select[name='issueType']").val();
	$(".dynamic").hide();
	if(issueType == 2){
		$("input[name='issueCondition.issueType']").val(issueType);
		$(".dynamic.issueType-recharge").show();
	}
	if(issueType == 3){
		$("input[name='issueCondition.issueType']").val(issueType);
		$(".dynamic.issueType-payOrder").show();
	}
}

function changeIssueTotalAmount(){
	var limitedType = $("input[name='issueTotalAmountlimited']:checked").val();
	if(limitedType == 'unlimited'){
		//只要选择未限制，就清空
		$("input[name='issueTotalAmount']").val("");
		$("input[name='issueTotalAmount']").attr("disabled","disabled");
	}else{
		$("input[name='issueTotalAmount']").removeAttr("disabled");
	}
}

function changeValidityType(){
	var validityType = $("input[name='validityType']:checked").val();
	if(validityType == 'absolute'){
		$("input[name='validDay']").val("");
		$("input[name='validDay']").attr("disabled","disabled");
		$("input[name='validBeginTime']").removeAttr("disabled");
		$("input[name='validEndTime']").removeAttr("disabled");
	}else if(validityType == 'relative'){
		$("input[name='validBeginTime']").val("");
		$("input[name='validEndTime']").val("");
		$("input[name='validBeginTime']").attr("disabled","disabled");
		$("input[name='validEndTime']").attr("disabled","disabled");
		$("input[name='validDay']").removeAttr("disabled");
	}
}

function checkvalue(obj){
	obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
	obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字
	obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数	
}
